package org.flow.basic.queue;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.UUID;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

/**
 * @author xl-9527
 * @since 2025/5/29
 */
public class BlockingQueueDemo {

    private static final Logger log = LoggerFactory.getLogger(BlockingQueueDemo.class);
    private final BlockingQueue<String> strQueue = new LinkedBlockingDeque<>(3);


    public void consumerPoll() {
        try {
            final String pollRes = strQueue.poll(1, TimeUnit.MICROSECONDS);
            log.info("消费消息 -> {}", pollRes);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }

    public void producerOffer() {
        if (strQueue.offer(UUID.randomUUID().toString())) {
            log.info("发送消息成功等待消费 _> {}", strQueue.size());
        }
    }
}
